package lib.bone.audit.workload.dao;

import lib.bone.audit.workload.domain.NepuWorkload;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface NepuWorkloadDAO extends JpaRepository<NepuWorkload,String> {

    /**
     * 根据用户ID 和 周期ID列表查询
     * @param userId
     * @param cycleIds
     * @return
     */
    List<NepuWorkload> findByUserIdAndCycleIdIn(String userId,List<String> cycleIds);


    /**
     * 通过userId 获取全部工作量
     * @param userId
     * @return
     */
    List<NepuWorkload> findByUserId(Long userId);


    /**
     * 根据周期id 和 用户id 和 工作量类型查询
     * @param cycleId
     * @param userId
     * @param workloadType
     * @return
     */
    List<NepuWorkload> findByCycleIdAndUserIdAndWorkloadType(String cycleId,Long userId,String workloadType);

    /**
     * 根据关联ID进行查询
     * @param linkId
     * @return
     */
    Integer countByLinkId(String linkId);

    /**
     * 根据关联ID 和 来源查询
     * @param linkId
     * @param workloadSource
     * @return
     */
    Optional<NepuWorkload> findByLinkIdAndWorkloadSource(String linkId,String workloadSource);


    /**
     * 查询该周期内，用户列表 （用户二级院查询）
     * @param pageable
     * @param cycleId
     * @param ids
     * @return
     */
    Page<NepuWorkload> findByCycleIdAndUserIdIn(Pageable pageable, String cycleId, List<Long> ids);

    /**
     * 查询全部周期信息
     * @param pageable
     * @param ids
     * @return
     */
    Page<NepuWorkload> findByUserIdIn(Pageable pageable,List<Long> ids);

    /**
     * 多条件查询
     * @param userId
     * @param workloadType
     * @param usedUp
     * @return
     */
    List<NepuWorkload> findByUserIdAndWorkloadTypeAndUsedUp(Long userId,String workloadType,Integer usedUp);
    List<NepuWorkload> findByUserIdAndWorkloadTypeAndUsedUpAndCycleId(Long userId,String workloadType,Integer usedUp,String cycleId);


    List<NepuWorkload> findByUserIdAndWorkloadTypeAndUsedUpOrderByWorkloadLevelDesc(Long userId,String workloadType,Integer usedUp);

    List<NepuWorkload> findByUserIdAndWorkloadTypeAndUsedUpOrderByWorkloadAsc(Long userId,String workloadType,Integer usedUp);


    /**
     * 获取用户全部未用尽的工作量记录
     */
    List<NepuWorkload> findByUserIdAndUsedUp(Long userId, Integer usedUp);



    List<NepuWorkload> findByLinkId(String linkId);


    List<NepuWorkload> findByLinkIdIn(List<String> linkIds);


    void deleteByIdIn(List<String> ids);
    

    List<NepuWorkload> findByUserIdAndCycleId(Long userId,String cycleId);

    List<NepuWorkload> findByIdIn(List<String> ids);

    @Query(value = "select * from nepu_workload where workload_type in('其它','教学工作量') and user_id = ?1 and used_up = ?2 and parent_id = ?3", nativeQuery = true)
    List<NepuWorkload> getTeachAndOther(Long userId,Integer usedUp,String cycleId);

    @Query(value = "select * from nepu_workload where link_Id = ?1 and workload_source = ?2 and Date(created_time) < '2023-01-01'", nativeQuery = true)
    Optional<NepuWorkload> getOldWorkload(String linkId,String workloadSource);




}
